package com.keur.money.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.adapter.MullaOnItemChildClickListener
import com.keur.money.base.adapter.MullaRecyclerViewAdapter
import com.keur.money.base.adapter.MullaViewHolderHelper
import com.keur.money.base.utils.DensityUtils
import com.keur.money.base.utils.ImageUtil
import com.keur.money.databinding.DialogMullaBindCardSubmitBinding
import com.keur.money.http.HttpHelper
import com.keur.money.utils.decoration.LinearSpacingItemDecoration
import com.keur.money.utils.event.KeurEventType
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/31 13:45
 * @Annotation：绑卡确认dialog
 */
class BindCardSubmitDialog: ProDialog {
    private lateinit var binding: DialogMullaBindCardSubmitBinding
    private var mCallback: IDialogCallback? = null

    private var itemAdapter: ItemAdapter? = null

    override fun initWindow() {
        val params = window?.attributes
        params?.width = MyApp.app.getPhoneWidth() - DensityUtils.dp2px(30f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.picker_view_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaBindCardSubmitBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose)
        setOnClickListener(R.id.btnConfirm)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        mCallback?.onClick(v)
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
        val topDecoration = DensityUtils.dp2px(12f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.recyclerView)
        binding.recyclerView.adapter = itemAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        itemAdapter?.setOnItemChildClickListener(MullaOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter?.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_mulla_account) {
        override fun setItemChildListener(helper: MullaViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.textName,model.optString("walletDesc"))
            helper.setText(R.id.textNumber,"+" + model.optString("walletMobile"))
            val imgLogo = helper.getImageView(R.id.imgLogo)
            ImageUtil.instance?.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), R.mipmap.icon_holder)

            //不显示删除
            helper.setVisibility(R.id.imgDelete,View.GONE)
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW,"")
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS,"")
    }
}